package com.example.javabasic.thread.classone.demo3;

import com.example.javabasic.spring.proxy.demo1.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * setDaemon  方法   设置为true的时候  守护线程执行完后  会直接关闭子线程
 */
public class ThreadDemoThree {

    public static volatile Integer x = 0;

    public static void main(String[] args) throws InterruptedException {
        test1();
//        Thread thread = new Thread(() -> {
//            test1();
//        }, "abc");
//        thread.start();
//
//        while(x<9){
//            System.out.println(x);
//            Thread.sleep(1000);
//        }
//        System.out.println(thread.getName()+":等待结束");
//        test2();
//        test3();
//        test4();

    }

    /**测试守护线程*/
    public static void test5(){
        Thread thread = new Thread(()->{

        });
    }

    /**
     * 测试 interrupt();
     */
    public static void test4() {
        //开启t 和t2   t中死循环    t2  两秒后 中断t
        //在下方等待t 运行完,
        Thread t = new Thread(() -> {
            while (true) {

            }
        }, "t");

        t.start();
        Thread main = Thread.currentThread();
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            main.interrupt();
            System.out.println("t  interrupt");
        });

        t2.start();


        try {
            System.out.println(t.getName());
            t.join();
            //这里由main 线程在等待  底层就是调用wait()方法
        } catch (InterruptedException e) {
            //终止异常
            e.printStackTrace();
            System.out.println("t 打断");
        }
    }

    /**
     * 测试join()
     */
    public static void test3() {
        Thread thread = new Thread(() -> {
            //遍历输出1到1000
//            IntStream.range(1,1000).forEach(i-> System.out.println(Thread.currentThread().getName()+"->"+i));
            try {
                Thread.sleep(1000);
                System.out.println(":输出内容");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3");
        thread.start();
        try {
            //等待thread 线程自行完毕,
//            thread.join();
            //等待100毫秒  ,如果还没执行完  主线程就继续往下执行后面的内容
            thread.join(100);
            System.out.println("线程执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    /**
     * Optional
     */
    public static void test2() {
        Thread thread = new Thread(() -> {
            try {
                Optional.of("hello").ifPresent(System.out::println);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        //名称
        Optional.of(thread.getName()).ifPresent(System.out::println);
        //id
        Optional.of(thread.getId()).ifPresent(System.out::println);
        //优先级
        Optional.of(thread.getPriority()).ifPresent(System.out::println);
    }

    /**
     * 测试deamon()方法的作用
     */
    public static void test1() throws InterruptedException {

        Thread thread = new Thread(
                () -> {
                    try {
                        System.out.println(Thread.currentThread().getName() + ":" + "start");
                        while(true){
                            System.out.println(x++);
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
        ,"守护线程");
        //  设置守护线程是否关闭子线程
        thread.setDaemon(true);
        thread.start();
        System.out.println(Thread.currentThread().getName() + ": over");
    }

//
//    public static void main(String[] args) {
//        new DemoThread(()->{
//            work();
//        },()->{
//            System.out.println("线程执行完了!!!");
//        }).start();
//    }

    public static void work(){
        System.out.println("执行业务");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class DemoThread extends Thread{
        private Runnable listener;
        DemoThread(){
            super();
        }
        DemoThread(Runnable runnable,Runnable listener){
            super(runnable);
            this.listener = listener;
        }
        @Override
        public void run() {
            super.run();
            if(listener!=null){
                listener.run();
            }
        }

    }

    public static class Demo{
        private Thread thread;
        Demo(){
            this.thread = Thread.currentThread();
            new Thread(()->{
                try {
                    thread.join();
                    listener();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        public void listener(){
            System.out.println("当thread 运行要结束的时候执行");
            //如果是 thread运行结束后执行 那么 需要再开启一个线程 用join();
        }

    }

    public void test(String key,Runnable runnable){
        Map<String,Runnable> map = new HashMap<>();
        map.put("mothod1",()->{
            System.out.println("method1");
        });
        map.put("mothod2",()->{
            System.out.println("method2");
        });
        map.put("mothod3",()->{
            System.out.println("method3");
        });
        map.put("mothod4",()->{
            System.out.println("method4");
        });
        map.put(key,runnable);


        map.get("method3").run();
    }

    public void  test7(){
        test("n",()->{
            System.out.println("n");
        });
    }


}
